package com.cskaoyan.config.zjhShiro;


import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.module.pojo.MarketUserExample;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class WeChatRealm extends AuthorizingRealm {
    @Autowired
    MarketUserMapper mapper;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken= ((UsernamePasswordToken) authenticationToken);
        String username = usernamePasswordToken.getUsername();
        MarketUserExample example = new MarketUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<MarketUser> marketUsers = mapper.selectByExample(example);
        if (marketUsers == null || marketUsers.size() == 0) {
            return null;
        }
        MarketUser marketUser = marketUsers.get(0);
        //来源于数据库的真实密码
        String password = marketUser.getPassword();
        //1.Shiro管理的用户信息，你需要Shiro管理什么样的用户信息，就提供什么用户信息
        //2.真实密码 → 根据token中提供的用户名查出来的 → 这个值在认证器中会和token中的password做比较
        //3.realm的名称 → 可以作为不同realm的区分，但其实没太大用处
        return new SimpleAuthenticationInfo(marketUser, password, this.getName());
    }
}
